//
//  LZJoinGroupDetialHeadCell.m
//  laziz_Consumer
//
//  Created by xbnzd on 16/11/18.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZJoinGroupDetialHeadCell.h"
#import "UIImageView+WebCache.h"
#import "LZGroupDetailModel.h"
#import "LZCustomerUserModel.h"
#import "LZGrabCouponAndFightGroupsDetialModel.h"
#import "LZGroupUser.h"
#import "LZGrouponUserView.h"
#import "Masonry.h"

@interface LZJoinGroupDetialHeadCell()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *memberNumLabel;
@property (weak, nonatomic) IBOutlet UIView *iconBgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconBgViewHeight;
@property (weak, nonatomic) IBOutlet UIImageView *groupLeaderIcon;
@property (weak, nonatomic) IBOutlet UILabel *groupLeaderNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *startGroupTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *labStartGroup;

@end

@implementation LZJoinGroupDetialHeadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.memberNumLabel.layer.cornerRadius = 8;
    self.memberNumLabel.layer.masksToBounds = YES;
    
    self.groupLeaderIcon.layer.cornerRadius = 22;
    self.groupLeaderIcon.layer.masksToBounds = YES;
    
    self.labStartGroup.text = LZLocalizedString(@"group_start_group", nil);
}

- (void)setModel:(LZGroupDetailModel *)model {
    if (_model) {
        return;
    }
    _model = model;

    // 团购名称
    self.titleLabel.text = model.groupName;
    
    // 参团人数
    [self setMembersLabelWithCurrentNum:model.peopleCount TotleNum:model.salesPromotion.grouponCount];
    
    self.titleLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - 30 - self.memberNumLabel.intrinsicContentSize.width;
    
    // 开团人头像
    [self.groupLeaderIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIBaseImageUrl,model.customerUser.userPhoto]] placeholderImage:[UIImage imageNamed:@"lz_user_placehoiderImage"]];
    
    // 开团人名称
    self.groupLeaderNameLabel.text = model.customerUser.nickname;
    
    // 开团时间
    self.startGroupTimeLabel.text = model.createTime;

    // 参团人员信息
    [self addJoinGroupMembersIcon:model.grouponUsers];

}

- (void)setMembersLabelWithCurrentNum:(NSString *)numStr TotleNum:(NSString *)totleNumStr
{
//    NSString *s = [NSString stringWithFormat:@"%@:",LZLocalizedString(@"group_members", nil)];
    NSString *str = @"";
    if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
        str = [NSString stringWithFormat:@"  %@\\%@  ",numStr,totleNumStr];
    }
    else {
        str = [NSString stringWithFormat:@"  %@/%@  ",numStr,totleNumStr];
    }
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attStr addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xF2871C, 1) range:NSMakeRange(2, numStr.length)];
    self.memberNumLabel.attributedText = attStr;
}

/**
 添加参团人员头像

 @param iconArr <#iconArr description#>
 */
- (void)addJoinGroupMembersIcon:(NSArray *)iconArr
{
    if (iconArr.count == 0) {
        self.iconBgViewHeight.constant = 0;
    }else{
        
        float padding = 19;

        float width = ((CGRectGetWidth([UIScreen mainScreen].bounds)-20)-padding*3)/4.0;
        
        int rowNum = 4; // 每行的图片的最大数
        
        UIView *lastView = nil;
        
        int row = 0;    // 行数
        int rank = 0;    // 列数
        
        for (int i = 0; i<iconArr.count; i++) {
            
            LZGrouponUserView *userView = [[LZGrouponUserView alloc] initWithGrouponUserModel:iconArr[i]];
            [self.iconBgView addSubview:userView];
            lastView = userView;
            
            row = i/rowNum+1;    // 行数
            rank = (i-(row-1)*rowNum)+1;    // 列数
            
            [userView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(width);
                make.height.mas_equalTo(userView.intrinsicContentSize.height);
                if (row == 1) {
                    make.top.equalTo(self.iconBgView.mas_top);
                }
                else {
                    make.top.equalTo(self.iconBgView.mas_top).offset((userView.intrinsicContentSize.height)*(row-1));
                }
                if (rank == 1) {
                    make.leading.equalTo(@0);
                }
                else {
                    make.leading.equalTo(self.iconBgView.mas_leading).offset((width+padding)*(rank-1));
                }
            }];
        }
        
        CGFloat bgViewHeight = row*width+(row-1)*padding;
        
        self.iconBgViewHeight.constant = bgViewHeight;
    }
    
}

- (CGSize)intrinsicContentSize {
    CGFloat totleHight = 20 + self.titleLabel.intrinsicContentSize.height + 20.5 + 20 + self.iconBgViewHeight.constant + 20 + self.timeDownLabel.intrinsicContentSize.height + 20 + 44 + 20;
    return CGSizeMake(SCREEN_WIDTH, totleHight);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (UIView*)getGrouponUsersView:(LZGroupUser*)userModel {
    UIView *view = [[UIView alloc] init];
    
        
    return view;
}

@end
